/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.hotTopicComment.service.impl;

import co.yixiang.api.ApiResult;
import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.modules.hotTopic.domain.YxHotTopic;
import co.yixiang.modules.hotTopic.service.YxHotTopicService;
import co.yixiang.modules.hotTopicComment.domain.YxComment;
import co.yixiang.modules.hotTopicComment.service.YxCommentService;
import co.yixiang.modules.hotTopicComment.service.dto.YxCommentDto;
import co.yixiang.modules.hotTopicComment.service.dto.YxCommentPage;
import co.yixiang.modules.hotTopicComment.service.dto.YxCommentQueryCriteria;
import co.yixiang.modules.hotTopicComment.service.mapper.YxCommentMapper;
import co.yixiang.modules.pay.zhifubao.EmptyUtils;
import co.yixiang.utils.FileUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
* @author hupeng
* @date 2021-08-12
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "yxComment")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxCommentServiceImpl extends BaseServiceImpl<YxCommentMapper, YxComment> implements YxCommentService {

    private final IGenerator generator;
    private final YxHotTopicService yxHotTopicService;

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(YxCommentQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YxComment> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", generator.convert(page.getList(), YxCommentDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<YxComment> queryAll(YxCommentQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(YxComment.class, criteria));
    }


    @Override
    public void download(List<YxCommentDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YxCommentDto yxComment : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("用户id", yxComment.getUid());
            map.put("热门话题id", yxComment.getHotTopicId());
            map.put("内容", yxComment.getContent());
            map.put("创建时间", yxComment.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }


    @Override
    public ApiResult<Page<YxCommentPage>> getHotTopicCommentPage(Integer hotTopicId, Integer page, Integer limit) {
        Page page1=new Page(page,limit);
        Page<YxCommentPage> paList = this.baseMapper.getHotTopicCommentPage(page1,hotTopicId);
        return ApiResult.ok(paList);
    }


    @Override
    public ApiResult saveHotTopicComment(Integer hotTopicId, String content, Long uid) {
        YxHotTopic yxHotTopic = yxHotTopicService.getById(hotTopicId);
        if(EmptyUtils.isEmpty(yxHotTopic)){
            return ApiResult.fail("未找到该热门话题");
        }
        YxComment yxComment = new YxComment();
        yxComment.setContent(content);
        yxComment.setHotTopicId(hotTopicId);
        yxComment.setUid(uid);
        yxComment.setCreateTime(new Date());
        this.baseMapper.insert(yxComment);

        yxHotTopic.setCommentsNumber(EmptyUtils.isNotEmpty(yxHotTopic.getCommentsNumber())?yxHotTopic.getCommentsNumber()+1:1);
        yxHotTopicService.updateById(yxHotTopic);

        return ApiResult.ok();
    }
}
